package com.yt.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yt.entity.User;
import com.yt.service.ConService;
import com.yt.util.CommonResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Classname ProController
 * @Description
 * @Date 2021/10/31 19:43
 * @Create by 杨涛
 */
@RestController
public class ProController {

    @Resource
    ConService conService;

    //热点配置，需要个服务名
    //fallback 负责程序错误异常配置
    //blockHandler 负责规则(控制台违规配置）
//    @SentinelResource(value = "hot", blockHandler ="myblockHandler" )
    @SentinelResource(value = "hot", blockHandler ="myblockHandler", fallback = "errorFun"
    ,exceptionsToIgnore ={ //exceptionsToIgnore  是可以被忽略的异常
            NullPointerException.class
    })
    @RequestMapping("/get/{id}")
    public CommonResult<User> getUserById(@PathVariable("id") long id) {
        CommonResult<User> res=conService.getUserById(id);
        if(res.getData()==null)
            throw new NullPointerException("没有用户");
        return res;

    }


    public CommonResult<User> myblockHandler(@PathVariable("id") long id,BlockException e) {
        return new CommonResult<>(555,"blockhan:::::::"+e.getMessage(),new User());
    }

    public CommonResult<User> errorFun(@PathVariable("id") long id, Throwable e) {
        return new CommonResult<>(444,"errorFun::::::::"+e.getMessage(),new User());
    }
}
